package main

import (
	"os/exec"

	"github.com/ying32/govcl/vcl"
	"github.com/ying32/govcl/vcl/types"
)

// 创建生成测试用例页面
func (f *MainForm) createTestCaseFrame(i int) {
	var (
		top int32 = commonPadding
	)

	// 创建容器
	f.contentFrame[i] = vcl.NewFrame(f)
	f.contentFrame[i].SetParent(f)
	f.contentFrame[i].SetAlign(types.AlClient)
	f.contentFrame[i].SetColor(contentColor)

	// 生成位置
	f.testCaseFrame.filePath = vcl.NewLabeledEdit(f)
	f.testCaseFrame.filePath.SetParent(f.contentFrame[i])
	f.testCaseFrame.filePath.EditLabel().SetCaption("文件位置")
	f.testCaseFrame.filePath.SetLabelPosition(types.LpLeft)
	f.testCaseFrame.filePath.SetLabelSpacing(commonPadding)
	f.testCaseFrame.filePath.SetReadOnly(true)
	f.testCaseFrame.filePath.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)
	selectBasePath := vcl.NewButton(f)
	selectBasePath.SetParent(f.contentFrame[i])
	selectBasePath.SetCaption("选择文件夹")
	selectBasePath.SetBounds(f.testCaseFrame.filePath.Left()+f.testCaseFrame.filePath.Width()+commonPadding, top, formButtonWidth, formItemHeight)
	selectBasePath.SetOnClick(func(sender vcl.IObject) {
		if ok, dir := vcl.SelectDirectory1(0); ok {
			f.testCaseFrame.filePath.SetText(dir)
		}
	})

	// 操作
	top += commonPadding*2 + formItemHeight
	execHandle := vcl.NewButton(f)
	execHandle.SetParent(f.contentFrame[i])
	execHandle.SetBounds(formLabelWidth+commonPadding, top, formButtonWidth, formItemHeight)
	execHandle.SetCaption("生成文档")
	execHandle.SetOnClick(func(sender vcl.IObject) {
		filePathText := f.testCaseFrame.filePath.Text()
		if len(filePathText) == 0 {
			vcl.ShowMessage("数据填写不完整")
			return
		}
		f.consoleForm.Show()
		go func() {
			f.runGenerateTestCase(f.testCaseFrame.filePath.Text(), func(log string) {
				vcl.ThreadSync(func() {
					f.consoleLog.Lines().Add(log)
				})
			})
			reloadConfigs(3)
		}()
	})

	openDirHandle := vcl.NewButton(f)
	openDirHandle.SetParent(f.contentFrame[i])
	openDirHandle.SetBounds(execHandle.Left()+execHandle.Width()+commonPadding, top, formButtonWidth, formItemHeight)
	openDirHandle.SetCaption("打开目录")
	openDirHandle.SetOnClick(func(sender vcl.IObject) {
		if len(f.testCaseFrame.filePath.Text()) == 0 {
			vcl.ShowMessage("未选择目录")
			return
		}
		_ = exec.Command(`cmd`, `/c`, `explorer`, f.testCaseFrame.filePath.Text()).Start()
	})
}
